package com.isoftstone.util.docment;

import com.isoftstone.merchant.entity.payment.Payment;
import com.isoftstone.util.DateUtil;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import java.io.File;
import java.io.FileOutputStream;
/**
 * @Author: zhaojg
 * @Description
 * @Date: Created in 14:20 2017/12/25
 * @Modified by:
 */
public class DocumentWrite {


    /**
     * 生成给csp发送的缴费详细信息
     *
     * @param acctNo 银行卡号
     * @param payment 缴费订单
     * @return
     */
    public static String xmlWriteMessage(String acctNo,Payment payment) throws Exception {

        //创建Helper类
        Document document = DocumentHelper.createDocument();
        //创建根节点
        Element utility_payment = document.addElement("UTILITY_PAYMENT");
        //增加const_head子节点
        Element const_head = utility_payment.addElement("CONST_HEAD");
        //创建const_head下面的子节点
        Element request_type = const_head.addElement("REQUEST_TYPE"); //请求类型
        Element request_merch = const_head.addElement("REQUEST_MERCH"); //交易渠道
        Element merch_no = const_head.addElement("MERCH_NO"); //商户号 corp_code
        Element trn_code = const_head.addElement("TRN_CODE"); //CSP交易码
        Element merinfo = const_head.addElement("MERINFO"); //商户信息 corp_name
        Element front_traceno = const_head.addElement("FRONT_TRACENO"); //快缴交易流水号
        Element front_date = const_head.addElement("FRONT_DATE"); //快缴交易日期
        Element front_time = const_head.addElement("FRONT_TIME"); //快缴交易时间

        //创建data_area子节点
        Element data_area = utility_payment.addElement("DATA_AREA");
        //创建data_area节点下面的子节点
        Element acct_no = data_area.addElement("ACCT_NO"); //缴费账号 acctNo
        Element cust_id = data_area.addElement("CUST_ID"); //缴费号码 feeId
        Element cust_name = data_area.addElement("CUST_NAME"); //姓名 feeUserName
        Element cust_addr = data_area.addElement("CUST_ADDR"); //地址 addr
        Element fee_firmon = data_area.addElement("FEE_FIRMON"); //欠费起始月份 该缴费生成时的创建时间createDate
        Element fee_amount = data_area.addElement("FEE_AMOUNT"); //欠费金额 total_amt
        Element pay_mode_desc = data_area.addElement("PAY_MODE_DESC");//缴费方式提醒 ""
        Element pay_amount = data_area.addElement("PAY_AMOUNT"); //缴费金额 total_amt
        Element dsf_filler = data_area.addElement("DSF_FILLER"); //缴费详情 remark

        //为const_head元素添加内容
        request_type.setText("0240");
        request_merch.setText("BOCSDOPN");
        merch_no.setText(payment.getCorpCode());
        trn_code.setText("mobile02");
        merinfo.setText(payment.getCorpName());
        front_traceno.setText(payment.getPayId().toString());
        front_date.setText("20151109");
        front_time.setText("101346");

        //为data_area元素添加内容
        acct_no.setText(acctNo);
        cust_id.setText(payment.getFeeId());
        cust_name.setText(payment.getFeeUserName());
        if (payment.getAddr() != null) {
            cust_addr.setText(payment.getAddr());
        }else{
            cust_addr.setText("");
        }
        fee_firmon.setText(DateUtil.dateToDateStr(payment.getCreateDate()));
        fee_amount.setText(payment.getTotalAmt().toString());
        pay_mode_desc.setText("");
        pay_amount.setText(payment.getTotalAmt().toString());
        if (payment.getRemark() != null) {
            dsf_filler.setText(payment.getRemark());
        }else{
            dsf_filler.setText("");
        }
        //实例化输出格式对象
        OutputFormat format = OutputFormat.createPrettyPrint();
        //设置输出编码
        format.setEncoding("GBK");
        //去掉xml声明后的空格
        format.setNewLineAfterDeclaration(false);
        //设置缩进
        format.setIndent(true);

        File xmlFile = File.createTempFile("temp", ".xml");//创建临时文件

        FileOutputStream fos = new FileOutputStream(xmlFile);

        String sendXml = null;
        try {
            //生成XMLWrite对象，构造函数中的参数为需要输出的文件流和格式
            XMLWriter writer = new XMLWriter(fos, format);
            //开始写入，write方法中包含上面创建的Document对象
            writer.write(document);
            SAXReader reader = new SAXReader();
            Document document1 = reader.read(xmlFile);
            sendXml = document1.asXML();

            /**
             * 测试报文
             */
            sendXml =
                    "<?xml version='1.0' encoding='gbk'?>\n" +
                    "<UTILITY_PAYMENT>\\n\" + \"\\t\n" +
                    "<CONST_HEAD>\\n\" + \"\\t\\t\n" +
                    "<REQUEST_TYPE>0240</REQUEST_TYPE>\\n\" + \"\\t\\t\n" +
                    "<REQUEST_MERCH>BOCSDOPN</REQUEST_MERCH>\\n\" + \"\\t\\t\n" +
                    "<MERCH_NO>3747A001</MERCH_NO>\\n\" + \"\\t\\t\n" +
                    "<TRN_CODE>mobile02</TRN_CODE>\\n\" + \"\\t\\t\n" +
                    "<MERINFO>山东淄博职业学院 13756363678</MERINFO>\\n\" + \"\\t\\t\n" +
                    "<FRONT_TRACENO>201510270000917</FRONT_TRACENO>\\n\" + \"\\t\\t\n" +
                    "<FRONT_DATE>20151109</FRONT_DATE>\\n\" + \"\\t\\t\n" +
                    "<FRONT_TIME>101346</FRONT_TIME>\\n\" + \"\\t</CONST_HEAD>\\n\" + \"\\t\n" +
                    "<DATA_AREA>\\n\" + \"\\t\\t\n" +
                    "<ACCT_NO>4563513600036690725</ACCT_NO>\\n\" + \"\\t\\t\n" +
                    "<CUST_ID>20151027025</CUST_ID>\\n\" + \"\\t\\t\n" +
                    "<CUST_NAME>张奎龙</CUST_NAME>\\n\" + \"\\t\\t\n" +
                    "<CUST_ADDR>15811531135</CUST_ADDR>\\n\" + \"\\t\\t\n" +
                    "<FEE_FIRMON>20151027</FEE_FIRMON>\\n\" + \"\\t\\t\n" +
                    "<FEE_AMOUNT>3200.00</FEE_AMOUNT>\\n\" + \"\\t\\t\n" +
                    "<PAY_MODE_DESC></PAY_MODE_DESC>\\n\" + \"\\t\\t\n" +
                    "<PAY_AMOUNT>3200.00</PAY_AMOUNT>\\n\" + \"\\t\\t\n" +
                    "<DSF_FILLER>计算机学院-计算机科学与技术-15级，张奎龙，学费缴费，2015学年</DSF_FILLER>\\n\" + \"\\t</DATA_AREA>\\n\" + \"</UTILITY_PAYMENT>";
            System.out.println("sendXml = " + sendXml);
        } finally {
            //关闭临时文件
            fos.flush();
            fos.close();
            xmlFile.delete();//程序退出时删除临时文件
        }

//        //创建需要写入的对象
//        File file = new File("E:\\CSPXML\\");
//        File dirFile = new File(file + File.separator + DateUtil.getSdfTimes());
//        if (!file.exists()) {
//            file.mkdirs();
//        }
//        if (!dirFile.exists()) {
//            dirFile.mkdirs();
//        }
//        //文件路径
//        String path = dirFile + File.separator + String.valueOf(payment.getPayId()) + ".xml";
        return sendXml;

    }

}
